import permision from "@/common/permission.js"
import loading from '@/components/xuan-loading/xuan-loading.vue'
import cLabel from "@/components/cat-label/cat-label.vue"
import fromItem from '@/components/from-item/from-item.vue'
import wbstree from '@/components/treeData/wbsTree/sevenq-lazy-tree' //wbs
// import multipleSelect from "@/components/multiple-select/multiple-select";

// import wx from "weixin-js-sdk";
export default {
	components: {
		loading,
		cLabel,
		fromItem,
		wbstree,
		// multipleSelect,//龄期多选
	},
	data() {
		return {
			showdialog: false,
			Checktypenum: "0", //保存时传取样类型  0自检1外委
			EquipnameList: [], //设备编码数组
			CompanynameList: [], //标段名称数组

			fromMsg: {
				fId: "",
				fCompanyid: "", //所属标段
				fCompanyname: "", //所属标段
				fProjectid: "", //项目id 隐藏
				fProjectname: "", //项目简称
				fEngineeringpartid: "", //工程部位id
				fEngineeringpartname: "", //工程部位
				fCheckcompany: "", //检测单位
				fEquipname: "", //设备编号
				fTaskno: "", //任务单号
				fCreatedates: "", //创建时间时分秒不可编辑
				fCreateuserid: "", //当前登录人id
				fCreateusername: "", //当前登录
				fChecktype: "", //检测类型
				fWitnessid: "", //委托编号id
				fWitnessno: "", //委托编号
				fNature: "", // 当前登录用户的机构性质
				userId: "", //当前登录人id
				userName: "", //当前登录人姓名

				fSpecimenage: '', //龄期
				fConcretelevel: "", //设计强度等级(MPa)
				fConcretetype: "", //是否为泵送混凝土
				fSurface: "", //测面状态
				fAngle: "", //测试角度

				fThicknessdesignvalue: "", //设计厚度
				fThickness: "", //垫块厚度
				fCorrectionfactor: "", //修正因子
				fDiameter: "", //钢筋直径
				fAllowdeviation1: "", //允许偏差(+mm)
				fAllowdeviation: "", //允许偏差(mm)
				isSubmit: "0", //0 保存 1提交
			}, //参数


			flagNum: 0, //下拉框/时间表单  0可填  -1不可填

			// loading样式
			width: '150rpx',
			height: '100rpx',
			shadeShow: false,
			backgroundColor: "transparent",
			themeColor: '#33CCCC',
			// loading样式

			type: '', //判断新增or编辑
			companyNature: true, //当前用户的是否为中心试验室
			clickN: 0,
			click: "",
			// detatilId: "",
			Inv: 0,
			num: 1,
			userInfo: {},
			
			// 委托单编号
			fMaterialindex: -1,
			// 委托单编号

			// wbs
			formData: {
				name: "",
			},
			header: {
				Authorization: uni.getStorageSync('token'),
			},
			success: ["data"],
			wbsRowData: null,
			wbsUrlFrom: {
				fCompanyid: "", //标段id 必填
				projectId: "", //项目id 
				parentId: -1 //父级id 第一级传-1  子级取上一级fId或fNodeid 必填
			},
			// wbs

			isScanCode: false,
			isLock: true
		};
	},

	onNavigationBarButtonTap(e) {},
	onShow() {},
	onLoad(options) {
		var that = this;
		uni.getStorage({
			key: 'userInfo',
			success: function(res) {
				that.userInfo = JSON.parse(res.data);
				that.wbsUrlFrom = {
					fCompanyid: that.userInfo.companyId, //标段id 必填
					projectId: "", //项目id 必填
					parentId: -1 //父级id 第一级传-1  子级取上一级fId或fNodeid 必填
				}
			}
		})

		if (options.type === 'add') {
			this.getDate();
			// 新增
			this.type = 'add';
			this.fromMsg.fId = '';
			this.fromMsg.fChecktype = "桩基检测";
			this.fromMsg.fCreateusername = this.userInfo.realName; //创建人
			this.fromMsg.fCreateuserid = this.userInfo.userId; //创建人id
			this.fromMsg.fProjectname = this.userInfo.projectFullName; //项目简称

			if (this.userInfo.companyNature == "中心试验室") {
				this.companyNature = false;
				this.getBaseTree(0);
			} else {
				this.companyNature = true;
				this.fromMsg.fCompanyname = this.userInfo.companyFullName;
				this.fromMsg.fCompanyid = this.userInfo.companyId;
				this.EquipList(this.fromMsg.fChecktype, 0)
			}
		} else if (options.id) {
			// 编辑 
			this.type = 'edit';
			this.fromMsg.fId = options.id
			this.getInfoMsg(options.id)
			this.getBaseTree(1);
		}
		// this.timeing = new Date().getTime();

		// #ifdef APP-PLUS
		this.playVideo()
		uni.$on('submit-page', (data) => {
			that.clickNum(data)
		})
		// #endif
		// #ifdef H5
		// this.appliance = 1
		// #endif

	},
	// 返回
	onBackPress(event) {
		uni.navigateTo({
			url: "/pages/apply/fieldTestManage/fieldPilesDetectedData/testing/index"
		});
		return true;
	},
	methods: {
		// 详情接口
		getInfoMsg(id) {
			this.$http.getFieldtestCheckTaskByFid({
				fId: id, //数据id
				fChecktype: "桩基检测"
			}).then(res => {
				if (res.errcode == 0) {
					var that = this;
					that.fromMsg = res.data.pileTask;
					if (this.userInfo.companyNature == "中心试验室") {
						this.companyNature = false;
					} else {
						this.companyNature = true;
						this.EquipList(this.fromMsg.fChecktype, 0)
					}
				} else {
					uni.showToast({
						title: "获取详情数据失败",
						icon: 'none',
						duration: 2000
					});
				}
			}, (err) => {
				uni.showToast({
					title: "获取详情数据失败",
					icon: 'none',
					duration: 2000
				});
			});
		},

		// 下拉框
		CompanynameChange(e) { // 标段名称下拉选择
			console.log("设备编码下拉选择", e);
			this.fromMsg.fCompanyname = e.label;
			this.fromMsg.fCompanyid = e.value;
			this.EquipList(this.fromMsg.fChecktype, 1);
			this.fromMsg.fEquipname = ""; //设备编号
			this.fromMsg.fCheckcompany = '';

		},
		EquipnameChange(e) { // 设备编码下拉选择
			console.log("设备编码下拉选择", e);
			this.fromMsg.fCheckcompany = e.value;
			this.fromMsg.fEquipname = e.label;
		},

		// 下拉框
		// 委托单携带信息
		EntrynumberFocus() { // 委托单编号
			if (this.userInfo.companyNature == "中心试验室") {
				uni.navigateTo({
					url: "/pages/apply/fieldTestManage/listPage?keyType=" + "成孔质量检测"
				});
			} else {
				uni.showToast({
					title: '该用户不可编辑"委托单编号"',
					icon: 'none',
					duration: 2000
				});
			}
			return false;
		},
		otherFun(object) {
			// console.log( object);
			if (!!object) {
				this.fromMsg.fWitnessno = object.fWtnumber;
				this.fromMsg.fWitnessid = object.fId;
				this.fromMsg.fCompanyid = object.fCompanyid; //施工单位id
				this.fromMsg.fCompanyname = object.fCompanyname; //施工单位名称
				this.fromMsg.fEngineeringpartid = object.fId; //工程部位id
				this.fromMsg.fEngineeringpartname = object.fEngineeringpartname; //工程部位
			}
		},
		// 委托单携带信息

		// 保存/提交
		clickNum(data) {
			this.resultFrom(data);
		},
		resultFrom(data) {
			this.fromMsg.isSubmit = data;
			this.fromMsg.userId = this.userInfo.userId; //当前登录人id
			this.fromMsg.userName = this.userInfo.realName; //当前登录人名称
			this.fromMsg.fNature = this.userInfo.companyNature;

			if (this.fromMsg.fCompanyname == "" || this.fromMsg.fCompanyname == null || this.fromMsg.fCompanyname ==
				undefined) {
				uni.showToast({
					title: '标段名称不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (this.fromMsg.fEquipname == "" || this.fromMsg.fEquipname == null || this.fromMsg.fEquipname ==
				undefined) {
				uni.showToast({
					title: '设备编号不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (this.fromMsg.fCheckcompany == "" || this.fromMsg.fCheckcompany == null || this.fromMsg
				.fCheckcompany == undefined) {
				uni.showToast({
					title: '检测单位不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (this.fromMsg.fEngineeringpartname == "" || this.fromMsg.fEngineeringpartname == null || this
				.fromMsg.fEngineeringpartname == undefined) {
				uni.showToast({
					title: '工程部位不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			}

			if (this.isLock == true) {
				this.$http.saveFieldtestCheckTaskAPP(this.fromMsg).then(res => {
					if (res.errcode == 0) {
						this.isLock = false;
						this.$refs.loadings.open();
						this.routerPush(data)
					} else {
						this.isLock = true;
						this.$refs.loadings.close();
						this.clickN = 0
					}
				})
			}
			this.isLock = false;
		},
		routerPush(data) {
			setTimeout(() => {
				if (data == '0') {
					uni.showToast({
						title: "保存成功",
						icon: 'none',
						duration: 2000
					});
				} else {
					uni.showToast({
						title: "提交成功",
						icon: 'none',
						duration: 2000

					});
				}
				let that = this;
				that.isLock = false;
				uni.redirectTo({
					url: "/pages/apply/fieldTestManage/fieldPilesDetectedData/testing/index?Inv=" +
						data,
					success: () => {
						that.click = false;
						that.$refs.loadings.close();
					}
				})
			}, 1500)
		},
		// 保存/提交


		// 原生窗体按钮显示
		playVideo() {
			let subNVue = uni.getSubNVueById('fieldPilesDetectedData')
			subNVue.show()
		},
		// 原生窗体按钮显示


		// 获取当前日期
		getDate() {
			var time = new Date();
			const y = time.getFullYear();
			const m = (time.getMonth() + 1 + "").padStart(2, "0");
			const d = (time.getDate() + "").padStart(2, "0");
			const hh = (time.getHours() + "").padStart(2, "0");
			const mm = (time.getMinutes() + "").padStart(2, "0");
			const ss = (time.getSeconds() + "").padStart(2, "0");
			var timer = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
			var timing = `${y}-${m}-${d}`;
			this.fromMsg.fCreatedates = timer;
		},
		// 获取当前日期


		// 字典项
		EquipList(data, num) { //设备编号
			var obj = {
				fCompanyid: this.fromMsg.fCompanyid, //左侧选中组织机构id
				page: 1, //当前页码
				pageSize: 500, //每页显示条数
				keyType: data
			};
			this.EquipnameList = [];
			this.$http.getPileFoundationTaskEquipList(obj).then(res => {
				if (res.errcode == 0) {
					res.data.equipList.list.map(item => {
						this.EquipnameList.push({
							label: item.fCheckequipname,
							value: item.fCheckcompanyname
						})
						if (num == 0) {
							this.fromMsg.fCheckcompany = res.data.equipList.list[0].fCheckcompanyname;
							this.fromMsg.fEquipname = res.data.equipList.list[0].fCheckequipname;
						}
					})
				} else {
					uni.showToast({
						title: "设备编号获取数据失败",
						icon: 'none',
						duration: 2000
					});
				}
			}, (err) => {
				uni.showToast({
					title: "字典项接口报错",
					icon: 'none',
					duration: 2000
				});
			});
		},
		getBaseTree(num) {
			var data = {
				companyId: this.userInfo.companyId,
				userId: this.userInfo.userId
			}
			this.CompanynameList = [];
			this.$http.getBaseSgCompanyList(data).then(res => {
				if (res.errcode == 0) {
					var BaseTree = res.data.userCompanyTreeList;
					BaseTree.map(item => {
						this.CompanynameList.push({
							label: item.fShortname,
							value: item.fCompanyid
						})
					})
					if (BaseTree.length != 0) {
						this.fromMsg.fCompanyname = this.CompanynameList[0].label;
						this.fromMsg.fCompanyid = this.CompanynameList[0].value;
						this.EquipList(this.fromMsg.fChecktype, num);
					}

				} else {
					uni.showToast({
						title: "设备编号获取数据失败",
						icon: 'none',
						duration: 2000
					});
				}
			}, (err) => {
				uni.showToast({
					title: "字典项接口报错",
					icon: 'none',
					duration: 2000
				});
			});

		},
		// 字典项


		// wbs
		poinblur(e) {
			if (this.wbsRowData != null) {
				if (e.detail.value != this.wbsRowData.nameFull) {
					this.fromMsg.fEngineeringpartname = e.detail.value; //工程部位
					this.fromMsg.fEngineeringpartid = ""; //工程部位id
				} else {
					this.fromMsg.fEngineeringpartname = this.wbsRowData.nameFull; //工程部位
					this.fromMsg.fEngineeringpartid = this.wbsRowData.id; //工程部位id
				}
			}
		},
		getWbsToken() {
			var that = this
			that.$http.getWbsToken().then(res => {
				uni.setStorage({
					key: 'getWbsToken',
					data: JSON.stringify(res.data.Authorizations)
				});
				that.ProjectlocationFocus(that.userInfo.companyId, "");
			})
		},
		ProjectlocationFocus(id, num) {
			this.$refs.sevenqLazy._show(); //弹窗模式传递此操作参数
		},
		//接口报错返回
		error(e) {
			// console.log(e)
		},
		//点击确定返回
		confirmWbs(data) {
			var name = [];
			var id = [];
			var fLongids = [];
			data.map(item => {
				name.push(item.name);
				id.push(item.id);
				fLongids.push(item.fLongids)
			})
			this.fromMsg.fEngineeringpartname = name.join(','); //工程部位
			this.fromMsg.fEngineeringpartid = id.join(','); //工程部位id
			this.fromMsg.fLocationid = fLongids.join(','); //工程部位id
			// console.log("confirmWbs", this.wbsRowData,e);
			// this.fromMsg.fEngineeringpartname = this.wbsRowData.nameFull; //工程部位
			// this.fromMsg.fEngineeringpartid = this.wbsRowData.id; //工程部位id
		},
		//点击取消返回
		cancelWbs(e) {
			// console.log("cancelWbs", e)
		},
		//选择子级行数据
		wbsRow(row) {
			console.log("wbsRow", row);
			this.wbsRowData = row;
		},
		// wbs

	}
}
